package com.leetcode.partition1;

import java.util.*;

/**
 * @author `RKC`
 * @date 2021/7/31 9:07
 */
public class LC90子集2 {

    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        return backtracking3(nums);
    }

    public static void main(String[] args) {
        int[] nums = {1, 1, 1, 2, 2};
        System.out.println(subsetsWithDup(nums));
    }

    //dfs
    private static List<List<Integer>> dfs(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        dfs(nums, result, 0, new ArrayList<>());
        return result;
    }

    private static void dfs(int[] nums, List<List<Integer>> result, int index, ArrayList<Integer> subset) {
        if (!result.contains(subset)) {
            result.add(new ArrayList<>(subset));
        }
        if (nums.length == index) return;
        for (int i = index; i < nums.length; i++) {
            if (i > index && nums[i] == nums[i - 1]) continue;
            subset.add(nums[i]);
            dfs(nums, result, i + 1, subset);
            subset.remove(subset.size() - 1);
        }
    }

    //回溯法
    private static List<List<Integer>> backtracking3(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> answer = new ArrayList<>();
        backtracking3(answer, new ArrayList<>(), nums, 0, new boolean[nums.length]);
        return answer;
    }

    private static void backtracking3(List<List<Integer>> answer, List<Integer> path, int[] nums, int startIndex, boolean[] used) {
        answer.add(new ArrayList<>(path));
        for (int i = startIndex; i < nums.length; i++) {
            //对同一树层用过的元素进行跳过
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
            path.add(nums[i]);
            used[i] = true;
            backtracking3(answer, path, nums, i + 1, used);
            path.remove(path.size() - 1);
            used[i] = false;
        }
    }

    private static List<List<Integer>> backtracking2(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> answer = new ArrayList<>();
        backtracking2(answer, new ArrayList<>(), nums, 0);
        return answer;
    }

    private static void backtracking2(List<List<Integer>> answer, List<Integer> path, int[] nums, int startIndex) {
        answer.add(new ArrayList<>(path));
        //定义set集合对同一结点下的本层去重
        Set<Integer> set = new HashSet<>();
        for (int i = startIndex; i < nums.length; i++) {
            if (set.contains(nums[i])) continue;            //如果出现过就跳过
            set.add(nums[i]);
            path.add(nums[i]);
            backtracking2(answer, path, nums, i + 1);
            path.remove(path.size() - 1);
        }
    }

    private static List<List<Integer>> backtracking1(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        result.add(new ArrayList<>());
        for (int i = 1; i <= nums.length; i++) {
            backtracking1(result, nums, i, 0, new ArrayList<>());
        }
        return result;
    }

    private static void backtracking1(List<List<Integer>> result, int[] nums, int length, int index, List<Integer> subset) {
        if (subset.size() == length && !result.contains(subset)) {
            result.add(new ArrayList<>(subset));
            return;
        }
        if (subset.size() == length) return;

        for (int i = index; i < nums.length; i++) {
            if (i > index && nums[i] == nums[i - 1]) continue;              //提前剪枝，因为在排序后，相同元素是挨着的，因此如果遍历的下一个元素是与当前元素相同，则没有必要进行递归
            subset.add(nums[i]);
            backtracking1(result, nums, length, i + 1, subset);
            subset.remove(subset.size() - 1);
        }
    }

    //扩展法
    public static List<List<Integer>> expansion(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        result.add(new ArrayList<>());

        for (int num : nums) {
            List<List<Integer>> temp = new ArrayList<>();
            for (List<Integer> item : result) {
                //取出每一项list复制并进行追加
                List<Integer> it = new ArrayList<>(item);
                it.add(num);
                if (!result.contains(it)) temp.add(it);
            }
            result.addAll(temp);
        }
        return result;
    }
}
